package game.editer.ui;

import hx.layout.AnchorLayout;
import hx.events.MouseEvent;
import hx.layout.AnchorLayoutData;
import hx.display.ImageLoader;
import hx.display.Image;
import hx.display.Quad;
import hx.display.Box;

/**
 * 通用按钮组件
 */
class CommonButton extends Box {
	public var quad = new Quad(18, 18, 0x666666);

	public var icon:ImageLoader;

	public var upColor:UInt = 0x666666;

	public function new(iconData:String) {
		super();

		var lineQuad = new Quad(20, 20, 0x2e2e2e);
		this.addChild(lineQuad);
		lineQuad.layoutData = AnchorLayoutData.fill(0);

		this.addChild(quad);
		quad.x = quad.y = 1;
		icon = new ImageLoader(iconData);
		this.addChild(icon);
		this.layout = new AnchorLayout();
		icon.layoutData = AnchorLayoutData.center();
		this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		this.addEventListener(MouseEvent.MOUSE_OVER, onOver);
		this.addEventListener(MouseEvent.MOUSE_OUT, onOut);
		this.mouseChildren = false;
		icon.scaleX = 12 / icon.width;
		icon.scaleY = 12 / icon.height;
	}

	public function setImage(image:String):Void {
		icon.data = image;
		icon.scaleX = icon.scaleY = 1;
		icon.scaleX = 12 / icon.width;
		icon.scaleY = 12 / icon.height;
	}

	private function onOver(event:MouseEvent) {
		quad.data = 0x757575;
	}

	private function onOut(event:MouseEvent) {
		quad.data = upColor;
	}

	override function onAddToStage() {
		super.onAddToStage();
		this.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
	}

	override function onRemoveToStage() {
		super.onRemoveToStage();
		this.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
	}

	private function onMouseDown(event:MouseEvent) {
		this.quad.data = 0x999999;
	}

	private function onMouseUp(event:MouseEvent) {
		this.quad.data = upColor;
	}
}
